<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:r="http://rules.independentsi.org">
	<xsl:output method="html" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<html>
			<head>
				<title>XSLT Test Report</title>
				<style>
body
{
	background-color :#e5e5cc;
	font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
	color: #424272;
}

table
{
	border-collapse:collapse;
}

td, th 
{
	font-size:1em;
	border:1px solid #424272;
	padding:3px 7px 2px 7px;
}

th, .othr
{
	font-size:1.1em;
	text-align:left;
	padding-top:5px;
	padding-bottom:4px;
	color:#ffffff;
}

tr:hover
{
	font-style:bold;
}

tr.alt td 
{
	color:#000000;
}

.thnormal, .othr
{
	background-color: #87B952;
}

.success
{
	background-color: #B5F6A3;
}

.failure, td.errors
{
	background-color: #F04043;
}

.notknown
{
	background-color: #CA9223;
}

.nottested
{
	background-color: #CAC223;
}

a:link
{
	text-decoration:none;
	color:#336699;
}

a:visited
{
	text-decoration:none;color:#6699cc;
}

a:hover
{
	text-decoration:underline;
	color:#3366cc;
}

a:active
{
	text-decoration:underline;
}

div.spacer {
	height: 50px;
	width: 50px;
}
				</style>
			</head>
			<body>
				<h1><xsl:value-of select="concat('Test report on ', tokenize(/r:Results/r:Meta/@xsltName,'/')[last()])"/></h1>
				<p/>
				<h3><xsl:value-of select="concat('carried out on ', /r:Results/r:Meta/@date)"/></h3>
				<a name="top"><h2>Summary</h2></a>
				<table width="50%">
					<tbody>
						<tr class="thnormal">
							<th align="left">File</th>
							<th align="left">Failure Count</th>
							<th align="left">Incomplete Test Count</th>
						</tr>
						<xsl:for-each select="/r:Results/r:Meta/r:ResultSet">
							<tr class="success">
								<td>
									<a>
										<xsl:attribute name="href" select="concat('#', @source)"/>
										<xsl:value-of select="@source"/>
									</a>
								</td>
								<td>
									<xsl:if test="(count(.//r:assertion[.='false']) + count(.//r:mapping[.='false'])) gt 0">
										<xsl:attribute name="class" select="'errors'"/>
									</xsl:if>
									<xsl:value-of select="count(.//r:assertion[.='false']) + count(.//r:mapping[.='false'])"/>
								</td>
								<td>
									<xsl:value-of select="count(.//r:assertion[matches(., '^Unable')]) + count(.//r:mapping[matches(., '^Unable')])"/>
								</td>
							</tr>
						</xsl:for-each>
						<a href="#untested" class="link">
							<tr class="notknown">
								<td colspan="3">Summary of tests not executed.</td>
							</tr>
						</a>
					</tbody>
				</table>
				<xsl:apply-templates select="*"/>
				<div class="spacer"/>
				<a name="untested"><h2>Rules not executed</h2></a>
				<table width="80%">
					<tbody>
						<tr>
							<th align="left" class="notknown">Tests not executed</th>
						</tr>
						<xsl:variable name="pairs">
							<xsl:sequence select="for $name in distinct-values(//r:mapping/@name) return concat($name,'=', string(count(//r:mapping[@name=$name and matches(text(), 'true|false')])))"/>
						</xsl:variable>
						<xsl:for-each select="for $name in distinct-values(//r:mapping/@name) return concat($name,'=', string(count(//r:mapping[@name=$name and matches(text(), 'true|false')])))">
							<xsl:if test="substring-after(., '=')='0'">
								<tr class="nottested">
									<td><xsl:value-of select="substring-before(., '=')"/></td>
								</tr>
							</xsl:if>
						</xsl:for-each>
						<xsl:for-each select="for $name in distinct-values(//r:assertion/@name) return concat($name,'=', string(count(//r:assertion[@name=$name and matches(text(), 'true|false')])))">
							<xsl:if test="substring-after(., '=')='0'">
								<tr class="nottested">
									<td><xsl:value-of select="substring-before(., '=')"/></td>
								</tr>
							</xsl:if>
						</xsl:for-each>
					</tbody>
				</table>
				<a href="#top">Top</a>
			</body>
		</html>
	</xsl:template>
	<xsl:template match="r:ResultSet">
		<h2>
			<a>
				<xsl:attribute name="name"><xsl:value-of select="@source"/></xsl:attribute>
				Test results for file: 
			</a>
			<a href="{concat(@sourcepath, '/', @source)}" target="_window"><xsl:value-of select="@source"/></a>
		</h2>
		<br/>
		<a href="{concat(@output, '/', @source,'.output.xml')}" target="_window">View Output</a>
		<table border="1" width="80%">
			<tbody>
				<tr class="thnormal">
					<th align="left" colspan="3">Test Name/Description</th>
					<th align="left">Test Result</th>
				</tr>
				<xsl:apply-templates select="*"/>
			</tbody>
		</table>
		<a href="#top">Top</a>
	</xsl:template>
	<xsl:template match="r:mapping">
		<tr>
			<xsl:attribute name="class">
				<xsl:choose>
					<xsl:when test=". = 'true'">success</xsl:when>
					<xsl:when test=". = 'false'">failure</xsl:when>
					<xsl:otherwise>nottested</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test=". = 'false'">
					<td><xsl:value-of select="@name"/></td>
					<td><xsl:value-of select="@sourceValue"/></td>
					<td><xsl:value-of select="@targetValue"/></td>
				</xsl:when>
				<xsl:otherwise>
					<td colspan="3"><xsl:value-of select="@name"/></td>
				</xsl:otherwise>
			</xsl:choose>
			<td>
			<xsl:choose>
				<xsl:when test=". = 'true'">
					<xsl:value-of select="'Success'"/>
				</xsl:when>
				<xsl:when test=". = 'false'">
					<xsl:value-of select="'Failure'"/>
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
			</xsl:choose>
			</td>
		</tr>
	</xsl:template>
	<xsl:template match="r:assertion">
		<tr>
			<xsl:attribute name="class">
				<xsl:choose>
					<xsl:when test=". = 'true'">success</xsl:when>
					<xsl:when test=". = 'false'">failure</xsl:when>
					<xsl:otherwise>nottested</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<td colspan="3"><xsl:value-of select="@name"/></td>
			<td>
			<xsl:choose>
				<xsl:when test=". = 'true'">
					<xsl:value-of select="'Success'"/>
				</xsl:when>
				<xsl:when test=". = 'false'">
					<xsl:value-of select="'Failure'"/>
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
			</xsl:choose></td>
		</tr>
	</xsl:template>
	<xsl:template match="r:because">
		<tr>
			<td colspan="4"><strong><xsl:value-of select="concat('Because ', @reason)"/></strong></td>
		</tr>
		<xsl:apply-templates select="*"/>
	</xsl:template>
	<xsl:template match="r:iteration">
		<tr class="othr">
			<td colspan="4"><strong><xsl:value-of select="concat('For &#34;', @each, '&#34;: instance ', @index)"/></strong></td>
			<!--td colspan="2">instance <xsl:value-of select="@index"/></td-->
		</tr>
		<xsl:apply-templates select="*"/>
		<tr class="othr">
			<td colspan="4">End instance <xsl:value-of select="@index"/></td>
		</tr>
	</xsl:template>
</xsl:stylesheet>
